package com.tomshidi.security.distributed.uaa.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author chenzhihua@skyworth.com
 * @version 1.0
 * @since 2022/5/11 19:28
 */

@RestController
@RequestMapping("/oauth")
public class BaseController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/captcha")
    public String captcha() {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        String key = new UUID(random.nextLong(), random.nextLong()).toString().replace("-", "");
        // 存入redis并设置过期时间为30分钟
        redisTemplate.opsForValue().set("base:auth::base:captcha:" + key, key, Duration.ofMinutes(30));
        // 将key和base64返回给前端
        return key;
    }

}
